library(MASS)
library(ggplot2)
library(mvtnorm)

mu <- c(0,0)
sigma <- matrix(c(1, 0.5, 0.5, 1), nrow = 2)

data <- mvrnorm(n = 1000, mu = mu, Sigma = sigma)

df <- data.frame(x=data[,1], y=data[,2])

ggplot(df, aes(x=x, y=y)) +
  stat_density_2d(aes(fill = ..level..), geom = "polygon", color = "white") +
  scale_fill_viridis_c() +
  coord_fixed() +
  theme_minimal() +
  ggtitle("Bivariate Normal Distribution") 

mu_x <- 0
mu_y <- 0
sigma_x <- 1
sigma_y <- 1

rho <- 0.5

cov_matrix <- matrix(c(sigma_x^2, rho * sigma_x * sigma_y, 
                     rho * sigma_x * sigma_y, sigma_y^2), nrow = 2)

x <- seq(-3, 3, length.out = 100)
y <- seq(-3, 3, length.out = 100)
grid <- expand.grid(x = x, y = y)

z <- mvtnorm::dmvnorm(grid, mean = c(mu_x, mu_y), sigma = cov_matrix)
Z <- matrix(z, nrow = length(x), ncol = length(y))

contour(x, y, Z, xlab = "X", ylab = "Y", main = paste("Bivariate Normal Distribution\n", "u_x=", mu_x, ", u_y=", mu_y, 
                                                      ", sigma_x=", sigma_x, ", sigma_y=", sigma_y, 
                                                      ", rho=", rho),
        col = "blue", nlevels = 10)


grid


1/3.5

0.175 / 0.05
